<table id="exam-info"></table>

<script>
	layui.use(["form", "table", "layer"], function (form, table, layer) {
		//用户编号 全局变量
		var userId = parseInt(instance.defaults.userId);
		table.render({
			elem: '#exam-info',
			height: 460,
			url: serverPath + "/exams/" + userId,
			headers: {
				"auth-token": getToken()
			},
			//res 即为原始返回的数据
			parseData: function (res) {
				console.log("考试表格原始返回的数据：")
				console.log(res)
				return {
					"code": res.data.code,
					"count": res.data.count,
					"data": res.data.data
				};
			},
			page: true,
			page: {
				limit: 5,
				limits: [5, 10, 15, 20]
			},
			cols: [
				[{
					field: '',
					title: '',
					align: "center",
					type: "checkbox"
				},
				{
					field: 'examId',
					title: '',
					align: "center",
					hide: true
				},
				{
					field: 'examNo',
					title: '考试编号',
					align: "center"
				},
				{
					field: 'examStatus',
					title: '考试状态',
					align: "center"
				},
				{
					field: 'examScore',
					title: '得分',
					align: "center"
				},
				{
					field: '',
					title: '操作',
					align: "center",
					templet: function (row) {
						return `
                        <button type="button" class="layui-btn layui-btn-normal" onclick="beginExam(this)" examNo="${row.examNo}" examStatus="${row.examStatus}">开始考试</button>
                        <button type="button" class="layui-btn layui-btn-danger" onclick="viewCheckResult(this)" examNo="${row.examNo}" examStatus="${row.examStatus}">查看批改结果</button>
                        `;
					}
				}
				]
			],
			done: function (res, curr, count) {
				if (curr > 1 && null == res.data) {
					curr = curr - 1;
					table.reload('exam-info', {
						//设定异步数据接口的额外参数，任意设
						where: {},
						page: {
							curr: curr
						}
					});
				}
			}
		});
		//开始考试
		window.beginExam = function (target) {
			var $target = $(target)
			var examNo = $target.attr("examNo");
			var examStatus = $target.attr("examStatus");
			if ("未考" == examStatus) {
				location.href = "paper-answer.html?examNo=" + examNo + "&userId=" + userId;
			} else {
				layer.alert("当前试卷" + examStatus)
			}

		}
		//查看批改结果
		window.viewCheckResult = function (target) {
			var $target = $(target)
			var examNo = $target.attr("examNo");
			var examStatus = $target.attr("examStatus");
			if ("过期" == examStatus) {
				layer.alert("该试卷已过期");
				return;
			}
			if ("未考" == examStatus) {
				layer.alert("请先完成该试卷")
			} else {
				location.href = "paper-result.html?examNo=" + examNo + "&userId=" + userId;
			}
		}
	})
</script>